#include <stdio.h>
#include "DBRecord.h"
#include "DBhistory.h"
#include "DeviceRecord.h"
//#define panelFile "./v17.0/PanelConfig.bin"
//#define panelFile "/DB/Config/v17.0/PanelConfig.bin"
#define m_panelFile "/DB/Config/v17.0/CommonConfig.bin"
#define m_newPanelFile "/DB/Config/v17.0/CommonConfig_new.bin"
//#define panelFile "./v17.0/CommonConfig.bin"
char ioBuffer[100000];

void ShowBuffer(char * buffer, int size);
int UpdateConfigFile(const char * configFile, const char * newConfigFile);
int ReadConfigFile(const char * configFile);

int main(int argc, char *argv[])
{
    UpdateConfigFile(m_panelFile,m_newPanelFile);
	ReadConfigFile(m_newPanelFile);
}

//fill in the cache of DeviceTable content 
int FillV18DeviceTableCache(char * deviceTable, int tableSize)
{
    memset(deviceTable,0,tableSize);
    DeviceRecord *  pDeviceRec = (DeviceRecord *)deviceTable;
    	
}

int UpdateConfigFile(const char* configFile,const char* newConfigFile)
{
   ConfigTableHeader configHeader;
   FILE * f, * fnew;
   f=fopen(configFile,"rt+");
   fnew=fopen(newConfigFile,"w");
   if(f==NULL)
   {
	   printf("failed to open %s\r\n",configFile);
	   return false;
   }
   else
   {
	   printf("open %s succesfully,\r\n",configFile);
   }

   int ibytesRead;
   int iTableID,iTableSize,iVersion;
   
   UINT16 iNumRecords,iRecordSize;
   memset(&configHeader,0,sizeof(ConfigTableHeader));
   while((ibytesRead = fread(&configHeader,1,sizeof(ConfigTableHeader),f))>0)
   {
	   if(ibytesRead!= sizeof(ConfigTableHeader))
	   {
		   printf("error reading record header file %s\r\n",configFile);
		   fclose(f);
		   fclose(fnew);
		   return false;
	   }

	   //show table header
	   iTableID = configHeader.TableID;
	   iNumRecords =DBRecord::Get16(configHeader.NumRecords);
	   iRecordSize= DBRecord::Get16(configHeader.RecordSize);
	   iTableSize=iNumRecords * iRecordSize;
	   iVersion=configHeader.VersionNumber;
	   printf("Config header,table id %d, version %d, record %d,recordSize %d, totalsize %d\r\n",iTableID,iVersion,iNumRecords,iRecordSize,iTableSize);
	   
	   //add table header into new file  
	   configHeader.VersionNumber = 18;
	   fwrite(&configHeader,1,sizeof(ConfigTableHeader),fnew);

	   //add table record into new file
	   memset(ioBuffer,0,sizeof(ioBuffer));
	   ibytesRead = fread(ioBuffer,1,iTableSize,f) ;
	   if(ibytesRead != iTableSize)
	   {
		   printf("error reading file record %s\r\n",configFile);
		   fclose(f);
		   fclose(fnew);
		   return false;
	   }
	   fwrite(ioBuffer,1,ibytesRead,fnew);
   }
   fclose(f);
   fclose(fnew);
   return true;
}

int ReadConfigFile(const char * configFile)
{
   ConfigTableHeader configHeader;
   FILE * f ;
   f=fopen(configFile,"rt+");
   if(f==NULL)
   {
	   printf("failed to open %s\r\n",configFile);
	   return false;
   }
   else
   {
	   printf("open %s succesfully,\r\n",configFile);
   }

   int ibytesRead;
   int iTableID,iTableSize,iVersion;
   
   UINT16 iNumRecords,iRecordSize;
   memset(&configHeader,0,sizeof(ConfigTableHeader));
   while((ibytesRead = fread(&configHeader,1,sizeof(ConfigTableHeader),f))>0)
   {
	   if(ibytesRead!= sizeof(ConfigTableHeader))
	   {
		   printf("error reading record header file %s\r\n",configFile);
		   fclose(f);
		   return false;
	   }

	   //show table header
	   iTableID = configHeader.TableID;
	   iNumRecords =DBRecord::Get16(configHeader.NumRecords);
	   iRecordSize= DBRecord::Get16(configHeader.RecordSize);
	   iTableSize=iNumRecords * iRecordSize;
	   iVersion=configHeader.VersionNumber;
	   printf("Config header,table id %d, version %d, record %d,recordSize %d, totalsize %d\r\n",iTableID,iVersion,iNumRecords,iRecordSize,iTableSize);
       fseek(f,iTableSize,SEEK_CUR);
   }
   fclose(f);
   return true;
}

void ShowBuffer(char * buffer, int size)
{
	int i;
	for(i=0;i<size;i++)
	{
		printf("%d",buffer[size]);
	}
}
